package sets.collection.domain;


import java.util.Objects;

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    @Override
    public int compareTo(Student o) {
        //System.out.println(this.age+"----"+o.age);
        //this.xxx - o.xxx :正序
        //o.xxx-this.xxx：倒序
        //先根据年龄做排序，再根据姓名
        int ageRe = this.age-o.age ;
        int nameresult = ageRe == 0 ? o.name.compareTo(this.name) : ageRe;
        //姓名年龄都一样，如果一样就会不存，返回1就可以存
        int result = nameresult==0? 1:nameresult;
        return result;
    }
    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    //重写equals方法，对象之间的比较会按照属性比，而不是地址
    @Override
    public boolean equals(Object o) {
        System.out.println("equals调用");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }
    public void setAge(int age)  {
        this.age = age;
    }
    public String toString() {
        return name  + age ;
    }
    @Override
    public int hashCode() {
        //把属性都参与到运算中
        return age+name.hashCode();
    }
}
